<?php
/**
 * 用餐管理控制器
 * 
 * @category QuickBug
 * @copyright http://www.vquickbug.com
 */
class DinnerController extends BaseController
{
	/**
	 * 模型
	 *
	 * @var unknown_type
	 */
	private $model;
	
	/**
	 * 自动运行
	 */
	public function init(){
		parent::init();
		if(!$this->is_operator){
			$this->msgbox('您无权限访问',url('index','index'));
		} 
		$this->model = new Model_Order_Dinner();
		
	}
	
	public function indexAction(){
		$this->view->controller = $this->controller;
	}
	
	public function checkoutAction(){
		$this->view->controller = $this->controller;
	}
	
	public function sumAction(){
		$this->view->controller = $this->controller;
	}
	
	public function listAction(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$offset = ($page-1)*$rows;
		$result = array();
		$result["total"] = $this->model->countOrderDinner();
		$result["rows"] = $this->model->listOrderDinner($offset,$rows);
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function comboboxuserAction(){
		$result = array();
		$result = $this->model->comboboxUser();
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function comboboxdepartmentAction(){
		$result = array();
		$result = $this->model->comboboxDepartment();
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function comboboxvisitorAction(){
		$result = array();
		$result = $this->model->comboboxVisitor();
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function addAction(){
		$param = array(
			'date'=>$this->request->getPost('date'),
			'vid'=>intval($this->request->getPost('vid')),
			'did'=>intval($this->request->getPost('did')),
			'uid'=>intval($this->request->getPost('uid')),
			'visitor'=>$this->model->getVname(intval($this->request->getPost('vid'))),
			'department'=>$this->model->getDname(intval($this->request->getPost('did'))),
			'user'=>$this->model->getUname(intval($this->request->getPost('uid'))),
			'class'=>intval($this->request->getPost('class')),
			'hc'=>$this->request->getPost('hc'),
			'tc'=>$this->request->getPost('tc'),
			'price'=>$this->request->getPost('price'),
			'drinks_price'=>$this->request->getPost('drinks_price'),
			'comment'=>$this->request->getPost('comment'),
			'created_at'=>time(),
			'created_by'=>$this->userid,
		);
		
		$result = $this->model->addOrderDinner($param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'添加申请失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function updateAction(){
		$id = intval($this->request->getGet('id'))?intval($this->request->getGet('id')):0;
		$param = array(
			'date'=>$this->request->getPost('date'),
			'vid'=>intval($this->request->getPost('vid')),
			'did'=>intval($this->request->getPost('did')),
			'uid'=>intval($this->request->getPost('uid')),
			'visitor'=>$this->model->getVname(intval($this->request->getPost('vid'))),
			'department'=>$this->model->getDname(intval($this->request->getPost('did'))),
			'user'=>$this->model->getUname(intval($this->request->getPost('uid'))),
			'class'=>intval($this->request->getPost('class')),
			'hc'=>$this->request->getPost('hc'),
			'tc'=>$this->request->getPost('tc'),
			'price'=>$this->request->getPost('price'),
			'drinks_price'=>$this->request->getPost('drinks_price'),
			'comment'=>$this->request->getPost('comment'),
			'modified_at'=>time(),
			'modified_by'=>$this->userid,
		);

		$result = $this->model->updateOrderDinner($id,$param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'编辑申请单失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function delAction(){
		$id = intval($this->request->getPost('id'))?intval($this->request->getPost('id')):0;
		$result = $this->model->delOrderDinner($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'取消申请单失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
}